#include "Win32.h"
#include "SceneFbx.h"
LRESULT WINAPI qWinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	LRESULT lRet = 1;
	switch( uMsg )
	{
	case WM_CREATE:
		break;
	case WM_PAINT:
		{
			qWin32 * qWin = (qWin32*)(LONG_PTR) GetWindowLongPtr( hWnd, GWL_USERDATA );
			if ( qWin )
			{
				qWin->DrawFrame();
			}
			ValidateRect( hWnd, NULL );
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_CHAR:
		{
			POINT pt;
			qWin32 * qWin = (qWin32*)(LONG_PTR) GetWindowLongPtr( hWnd, GWL_USERDATA );
			GetCursorPos( &pt );
			if ( qWin )
			{
				qWin->KeyUpdate((unsigned char )wParam, (int)pt.x, (int)pt.y );
			}
		}
		break;
	default:
		lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
		break;
	}
	return lRet;
}


qWin32::qWin32( const char* strTitle, int iWidth, int iHeight )
:m_iWidth(iWidth)
,m_iHeight(iHeight)
,m_pSceneFbx(NULL)
{
	assert(strTitle);
	int len = strlen(strTitle);
	memcpy( m_cTitle, strTitle, len );
	m_cTitle[len + 1] = '\0';
}

qWin32::~qWin32()
{
	ReleaseVideoDevice();
}

bool qWin32::Initilize()
{
	return ( InitWindow( 0, m_cTitle, m_iWidth, m_iHeight ) && InitVideoDevice() && InitDrawScene() );
}

void qWin32::MainLoop()
{
	MSG msg = {0};
	DWORD lastTime = ::GetTickCount();
	bool	bDone = true;
	float	delta = 0;
	while ( bDone )
	{
		int pm = (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ) != 0 );
		DWORD curTime = ::GetTickCount();
		delta += (float)(curTime - lastTime);
		lastTime = curTime;

		if ( pm )
		{
			if ( msg.message == WM_QUIT )
			{
				bDone = false;
			}
			else
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		}
		else
		{
			SendMessage( m_hWnd, WM_PAINT, 0, 0 );
		}

		UpdateFrame();
		
	}
}

void qWin32::DrawFrame()
{
	glClearColor( 1.0, 1.0, 0.0, 0.0 );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	m_ESGL.CompleteDrawESGL();
}
void qWin32::UpdateFrame()
{

}

void qWin32::KeyUpdate( unsigned char key, int x, int y )
{

}

bool qWin32::InitWindow( HINSTANCE hInstance, const char * cTitle, int iWidth, int iHeigth )
{
	WNDCLASS	wc = {0};	
	DWORD		ws = 0;
	RECT		wr;
	HINSTANCE	hInst = (hInstance != 0) ? hInstance: GetModuleHandle(NULL);
	wc.style			= CS_OWNDC;
	wc.lpfnWndProc		= (WNDPROC)qWinProc; 
	wc.hInstance		= hInst;
	wc.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszClassName	= "quses";

	if( !RegisterClass(&wc) )
	{
		printf("RegisterClass Error!\n");
		return false;
	}

	ws = WS_VISIBLE | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION;

	wr.left		= 0;
	wr.top		= 0;
	wr.right	= m_iWidth;
	wr.bottom	= m_iHeight;

	AdjustWindowRect( &wr, ws, FALSE );

	m_hWnd = CreateWindow( "quses",
		m_cTitle,
		ws,
		0,
		0,
		wr.right - wr.left,
		wr.bottom - wr.top,
		NULL,
		NULL,
		hInst,
		NULL );
	
	SetWindowLong( m_hWnd, GWL_USERDATA, (LONG)(LONG_PTR) this );

	if ( m_hWnd == NULL )
	{
		printf("Create Window Error!\n");
		return false;
	}
	return true;
}

bool qWin32::InitVideoDevice()
{
	HDC hDc = GetDC( m_hWnd );
	//HBITMAP hBitmap = ::CreateCompatibleBitmap( hDc, 600, 400 );
	if ( !m_ESGL.InitESGL( m_hWnd, hDc ) )
	{
		printf("Initilize Device error\n");
		return false;
	}
	return true;
}

bool qWin32::InitDrawScene()
{
	if ( m_pSceneFbx == NULL )
	{
		m_pSceneFbx = new CSceneFbx();
	}

	return m_pSceneFbx->InitScene("box.fbx");
}

void qWin32::ReleaseVideoDevice()
{
	m_ESGL.ReleaseESGL();
}